package com.yusong.algorithm.link;

import java.util.*;

/**
 * LRU最简式实现，LinkHashMap
 * https://leetcode-cn.com/problems/lru-cache/solution/lruhuan-cun-ji-zhi-by-leetcode-solution/
 * LinkedHashMap有现成实现
 */
public class LRUCache146 extends LinkedHashMap<Integer, Integer>{


    private int capacity;

    public LRUCache146(int capacity) {
        //Access Order 根据访问排序
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    public int get(Integer key) {
        return super.getOrDefault(key, -1);
    }

    public void put(int key, int value) {
        super.put(key, value);
    }

    /**
     * 修改LinkHashMap的删除元素的实现即可
     */
    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
        return size() > capacity;
    }


}
